"""
基于rest Framework判断用户是否登录
"""

from rest_framework.request import exceptions
from ..models import  UserInfo,UserToken
from rest_framework.authentication import BasicAuthentication



class FirstAuthtication(BasicAuthentication):
    def authenticate(self,request):
        pass
    def authenticate_header(self, request):
        pass

class Authtication(BasicAuthentication):
    # 定义一个函数
    def authenticate(self,request):
        # 获取token
        token = request._request.GET.get("token")
        # 从数据库中查看，如果为空则返回数据
        token_obj = UserToken.objects.filter(token=token).first()
        if not token_obj:
            raise exceptions.AuthenticationFailed("用户认证失败")

        # 如果认证成功，在rest framework内部会将整个两个字段赋值给request，以供后续操作使用
        return (token_obj.user,token_obj)

    def authenticate_header(self,request):
        pass